//
//  PBSuggestDetailController.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/2.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBSuggestDetailController.h"
#import "IQTextView.h"

@interface PBSuggestDetailController ()<UITextViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *topBgView;
@property (strong, nonatomic) IBOutlet UILabel *topLabel;
@property (strong, nonatomic) IBOutlet IQTextView *textView;
@property (nonatomic, assign) NSInteger bizType;

@end

@implementation PBSuggestDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self configureData];
}

- (void)configureView {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseTypeMethod)];
    [self.topBgView addGestureRecognizer:tap];
    self.textView.placeholder = @"请详细描写事情经过，不少于15字";
    self.textView.delegate = self;
}
- (void)configureData {
    
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){
        [self.view endEditing:YES];
        return NO;
    }
    return YES;
}

// 选择投诉类型
- (void)chooseTypeMethod {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择类型" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"家电" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.topLabel.text = @"选择类型    家电";
        self.bizType = 1;
    }];
    UIAlertAction *lifeAction = [UIAlertAction actionWithTitle:@"生活" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.topLabel.text = @"选择类型    生活";
        self.bizType = 2;
    }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"服务" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.topLabel.text = @"选择类型    服务";
        self.bizType = 3;
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
    }];
    
    [alertController addAction:okAction];
    [alertController addAction:lifeAction];
    [alertController addAction:otherAction];
    [alertController addAction:cancelAction];
    
    // 出现
    [self presentViewController:alertController animated:YES completion:^{
        NSLog(@"presented");
    }];
}

// 提交
- (IBAction)commitButtonClick:(id)sender {
    if (self.bizType == 0) {
        [self showHint:@"请先选择类型"];
        return;
    }
    
    if (self.textView.text.length < 15) {
        [self showHint:@"详细内容不能少于15字"];
        return;
    }
    
    NSDictionary *param = @{@"bizContent":self.textView.text,
                            @"bizType":[NSString stringWithFormat:@"%zd",self.bizType],
                            @"type":[NSString stringWithFormat:@"%zd",self.type]};
    [TMNetwork requestPostWithParam:param url:@"/user/userComplaintSuggest" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            [self showHint:@"提交成功,我们会尽快处理"];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [self showHint:model.message];
        }
    }];
}


#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    if (self.type == 1) {
        self.title = @"我要投诉";
    }else if (self.type == 2) {
        self.title = @"我有建议";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
